package de.amaly.wosdemdes.activity;

import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
import de.amaly.wosdemdes.R;
import de.amaly.wosdemdes.data.ShowProgressEntry;
import de.amaly.wosdemdes.persistence.ShowProgressEntryDAO;
import de.amaly.wosdemdes.ui.ShowProgressEntryArrayAdapter;

/**
 * This is the main (launcher) activity of the WoSDeMDeS (Wo Samma Denn Mid Dera Serie?) Android App.
 * 
 * It is responsible for listing all entered information about show watch progress and allows input elements for
 * modification of the stored progress.
 * 
 * @author Andreas Maly
 */
public class ShowsListActivity extends ListActivity {

    /** DAO reference for accessing the database */
    private ShowProgressEntryDAO dao;

    /** Constant for passing {@link ShowProgressEntry} intents around */
    public final static String EXTRA_SHOW_PROGRESS_ENTRY = "de.amaly.wosdemdes.SHOW_PROGRESS_ENTRY";

    /** Request code for calling the {@link CreateEditShowProgress} activity */
    public final static int EDIT_SHOW_PROGRESS_REQUEST = 0;

    private View undoContainer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shows_list);

        undoContainer = findViewById(R.id.undobar);

        dao = new ShowProgressEntryDAO(this);

        updateListAdapter();

        ListView listView = getListView();
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ShowProgressEntry entry = (ShowProgressEntry) parent.getAdapter().getItem(position);
                dao.open();
                dao.deleteShowProgressEntry(entry.getId());
                dao.close();
                updateListAdapter();
                parent.refreshDrawableState();
                undoContainer.setTag(entry);
                showUndo(undoContainer);
                return true;
            }
        });
    }


    private void updateListAdapter() {
        dao.open();
        List<ShowProgressEntry> entries = dao.getAllShowProgressEntries();
        dao.close();
        ShowProgressEntryArrayAdapter arrayAdapter = new ShowProgressEntryArrayAdapter(this, entries);
        setListAdapter(arrayAdapter);
    }


    /**
     * Define the {@link ActionBar} menu we want to incorporate into this {@link Activity}.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.shows_list_actions, menu);
        return true;
    }


    /**
     * Handle {@link ActionBar} action selection.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_add) {
            // Start the activity for creating the new ShowProgressEntry
            Intent intent = new Intent(this, CreateEditShowProgress.class);
            startActivityForResult(intent, EDIT_SHOW_PROGRESS_REQUEST);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Refresh the list(activity) to display the changes made by the finished activity
        if (resultCode == RESULT_OK) {
            finish();
            startActivity(getIntent());
        }
    }


    public static void showUndo(final View viewContainer) {
        viewContainer.setVisibility(View.VISIBLE);
        viewContainer.setAlpha(1);
        viewContainer.animate().alpha(0.4f).setDuration(10000).withEndAction(new Runnable() {

            @Override
            public void run() {
                viewContainer.setVisibility(View.GONE);
            }
        });
    }


    public void undoDeletion(View view) {
        ShowProgressEntry entry = (ShowProgressEntry) undoContainer.getTag();
        Toast toast = Toast.makeText(this, R.string.entry_restored, Toast.LENGTH_LONG);
        toast.show();
        dao.open();
        entry.setId(null);
        dao.saveShowProgressEntry(entry);
        dao.close();
        updateListAdapter();
        undoContainer.setVisibility(View.GONE);
    }

}
